$(function(){
	var $addEmailForm2= $('#addEmailForm2');
	
	//邮箱验证
	jQuery.validator.addMethod("isEmail", function(value, element) {
		var code = 0;
		$.ajax({
			type:'post',
			data:"email="+value,
			async: false,
			url:rootPath+'/user/info/data/isNameOrEmailUse.do',
			success:function(r){
				code = eval(r).code;
			}
		});
	    return code==0?true:false;
	}, "邮箱已使用，请更换邮箱");
	
    //用户名验证
	jQuery.validator.addMethod("isUserNameUse", function(value, element) {
		var code = 0;
		$.ajax({
			type:'post',
			data:"userName="+value,
			async: false,
			url:rootPath+'/user/info/data/isNameOrEmailUse.do',
			success:function(r){
				code = eval(r).code;
			}
		});
	    return code==0?true:false;
	}, "用户名已使用，请更换用户名");
	
/*	jQuery.validator.addMethod("isUserName", function(value, element) {
		return /^[a-zA-Z_0-9]{4,15}$/.test(value);
	}, "用户名格式不正确");*/
	
	
	jQuery.validator.addMethod("isUserNameOK", function(value, element) {
		return /^[a-zA-Z_0-9\u4e00-\u9fa5]+/.test(value);
	}, "用户名格式不正确,含有非法字符");
	
	jQuery.validator.addMethod("isUserNameLengthOK", function(value, element) {		
		var unlen = value.replace(/[^\x00-\xff]/g, "**").length;
		if(unlen < 4 || unlen > 15) {
			return false ;
		}
		return true ;
			
	}, "4到15个字符，一个汉字为两个字符");
	
	
	$addEmailForm2.validate({
		rules:{
			pluginUserName:{
				required: true,
				isUserNameOK:true,
				isUserNameLengthOK:true,
				isUserNameUse:true
			},
			pluginEmail:{
				required: true,
				email: true,
				isEmail:true
			},
			pluginPassword:{
				required: true,
				rangelength:[6,15]
			}
		
		},
		messages:{
			pluginUserName:{
				required: "用户名不能为空"			
			},
			pluginEmail:{
				required: "Email不能为空",
				email: "邮箱地址格式不正确"
			},
			pluginPassword:{
				required: "密码不能为空",
				rangelength: "密码须为{0}到{1}位"
			}
		},
		 errorElement: "span",
		 errorClass:'text_error',
		 errorPlacement: function(error, element) {
			 $('#serror').empty();
			 element.attr('class','text text_error');
			 var errorBox = element.parent().parent().find('.errorBox').empty();
			 errorBox.append('<img class="ui-icon ui-icon-error" src="'+rootPath+'/images/ucenter/blank.png" />&nbsp;'+error.html());
		 },
		 success: function(label,d) {
				var parent = $(d).parent().parent().find('.errorBox').empty();
		},

		onclick: false
		 
	});	
	
	
	$('#addEmailForm2Submit').click(function(e){
		e.preventDefault();
		if ($addEmailForm2.valid()) {
			   var url = rootPath+"/user/info/data/addEmail2.do";
			   var params = "email="+$("#pluginEmail").val()+"&password="+$('#pluginPassword').val()+"&userName="+$('#pluginUserName').val();
			   $.ajax({
		        type: "get",
		        url: url,
		        data: params,
		        dataType:"json",
		        success: function(httpObj){		        	  
		        	   if(httpObj.code!=10011){		        		 
		        		 $("#editPwdSucess").hide();
		        		 $("#editPwdError").css({height : 0,padding:0}).show().stop().animate({height : '16px',paddingTop:'8px',paddingBottom:'8px'});     
		        		 $("#editPwdError").html("&nbsp;&nbsp;"+httpObj.data);		        		
		        	   }else{
		        		  $("#editPwdError").hide();
		        		  $('#J_loginDlg').hide().next().hide();
			        	 
		        		  var $script = $('<span>'+httpObj.data+'</span>').find('script');
		      			  if($script.size() <= 0)
		      				 location.href =rootPath+"/user/info/editInfo.htm";
		      			  var count = 0;
		      			   $script.each(function(index, domEle){
		      				 $.getScript($(domEle).attr('src'),function(response,status){		      					
		      					 count = count + 1;
		      				});
		      			  });
		      			   
		      			   
		      			 setInterval(function(){
		      				 if(count == $script.size())
		      					 location.href =rootPath+"/user/info/editInfo.htm";
  						},100);		      			   
			        	 
		        	   }
		        	$('#password').val('');
				}
		    });
		}
	});
	

	
});